package demoMod.invincibleOne.ui;

import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector2;
import com.megacrit.cardcrawl.core.CardCrawlGame;
import com.megacrit.cardcrawl.core.Settings;
import com.megacrit.cardcrawl.dungeons.AbstractDungeon;
import com.megacrit.cardcrawl.helpers.FontHelper;
import com.megacrit.cardcrawl.helpers.Hitbox;
import com.megacrit.cardcrawl.helpers.ImageMaster;
import com.megacrit.cardcrawl.helpers.MathHelper;
import com.megacrit.cardcrawl.helpers.input.InputHelper;
import com.megacrit.cardcrawl.relics.AbstractRelic;
import demoMod.invincibleOne.InvincibleOneMod;
import demoMod.invincibleOne.relics.TaskGoal;

public class BingoSlot {
    private BingoItem bingoItem;
    private float x;
    private float y;
    private float targetX;
    private float targetY;
    public Hitbox hb;
    // 部分情况可以点击bingo来完成该Bingo任务。
    public static boolean canInteract;

    public BingoSlot(BingoItem bingoItem) {
        this.bingoItem = bingoItem;
        hb = new Hitbox(Settings.WIDTH * 0.1F, Settings.WIDTH * 0.1F);
        bingoItem.setCurrentSlot(this);
    }

    public void update() {
        int index = InvincibleOneMod.bingoPanel.getSlots().indexOf(this);
        int line = index / BingoPanel.LINES;
        int column = index % BingoPanel.COLUMNS;
        targetX = Settings.WIDTH * 0.4F + column * Settings.WIDTH * 0.1F;
        targetY = Settings.HEIGHT / 2.0F + Settings.WIDTH * 0.15F - line * Settings.WIDTH * 0.1F;
        if (InvincibleOneMod.bingoPanel.hide) {
            if (AbstractDungeon.player.hasRelic(TaskGoal.ID)) {
                AbstractRelic relic = AbstractDungeon.player.getRelic(TaskGoal.ID);
                targetX = relic.currentX;
                targetY = relic.currentY;
            } else {
                targetX = 0;
                targetY = 0;
            }
        }
        x = MathHelper.orbLerpSnap(x, targetX);
        y = MathHelper.orbLerpSnap(y, targetY);
        hb.move(x + Settings.WIDTH * 0.05F, y + Settings.WIDTH * 0.05F);
        hb.update();
        if (this.hb.hovered && !InvincibleOneMod.bingoPanel.hide) {
            this.targetX = 25.0F * Settings.scale;
            if (InputHelper.justClickedLeft) {
                CardCrawlGame.sound.playA("UI_CLICK_1", -0.1F);
                this.hb.clickStarted = true;
                if (canInteract && bingoItem.status == 0 && !bingoItem.isReward) {
                    bingoItem.completeBingo();
                    canInteract = false;
                    InvincibleOneMod.bingoPanel.hide = true;
                }
            }
        }
        bingoItem.update();
    }

    public void render(SpriteBatch sb, float alpha) {
        sb.setColor(1.0F, 1.0F, 1.0F, alpha);
        sb.draw(ImageMaster.KEYWORD_TOP, x, y - 32.0F * Settings.scale + Settings.WIDTH * 0.1F, Settings.WIDTH * 0.1F, 32.0F * Settings.scale);
        sb.draw(ImageMaster.KEYWORD_BODY, x, y + 32.0F * Settings.scale, Settings.WIDTH * 0.1F, Settings.WIDTH * 0.1F - 64.0F * Settings.scale);
        sb.draw(ImageMaster.KEYWORD_BOT, x, y, Settings.WIDTH * 0.1F, 32.0F * Settings.scale);

        Color fontColor;
        if (bingoItem.getStatus() == 0) {
            fontColor = Color.WHITE.cpy();
        } else if (bingoItem.getStatus() == 1) {
            fontColor = Color.CHARTREUSE.cpy();
        } else {
            fontColor = Color.RED.cpy();
        }
        fontColor.a = alpha;

        FontHelper.renderSmartText(sb, FontHelper.tipBodyFont, bingoItem.getDescription(), 22.0F * Settings.scale + x, y - 20.0F * Settings.scale + Settings.WIDTH * 0.1F, Settings.WIDTH * 0.1F - 44.0F * Settings.scale, 26.0F * Settings.scale, fontColor);
        hb.render(sb);
    }

    public BingoItem getBingoItem() {
        return bingoItem;
    }

    public void setBingoItem(BingoItem bingoItem) {
        this.bingoItem = bingoItem;
    }
}
